package com.vanguard.controller;

import com.vanguard.commons.base.BaseController;
import com.vanguard.commons.constant.CommonReturnCode;
import com.vanguard.commons.exception.BusinessException;
import com.vanguard.commons.pojo.ResponseResult;
import com.vanguard.commons.utils.CookieUtils;
import com.vanguard.commons.utils.LoggerUtils;
import com.vanguard.domain.Appointment;
import com.vanguard.domain.vo.AppointmentVo;
import com.vanguard.service.IAppointmentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Title: 预约前端控制器
 * @Description:
 * @Author: vanguard
 * @Version: 1.0
 * @Date: 2018/11/12
 */
@Controller
@RequestMapping("/appoint")
public class AppointmentController extends BaseController {

    @Autowired
    private IAppointmentService appointmentService;

    /**
     * 跳转到我的预约页面
     * @author vanguard
     * @date 16:04 2018/11/16
     * @param []
     * @return java.lang.Object
     */
    @RequestMapping(value = "/myAppoint", method = RequestMethod.GET)
    public Object myAppoint(Model model) {
        List<AppointmentVo> appointmentList = null;
        //获得Cookie中的openId
        String openId = CookieUtils.getCookieValue(getRequest(), "openId");
        try {
            appointmentList = appointmentService.findByOpenId(openId);
        } catch (BusinessException e) {
            model.addAttribute("errmsg", "查询我的预约信息失败");
            LoggerUtils.error(AppointmentController.class, e.getMessage());
        }
        model.addAttribute("appointmentList", appointmentList);
        return "/myappointment";
    }

    /**
     * 跳转到预约页面，并把openId传入页面
     * @param model
     * @return
     */
    @RequestMapping("/add")
    public String appointment(Model model) {
        //获得Cookie中的openId
        String openId = CookieUtils.getCookieValue(getRequest(), "openId");
        model.addAttribute("openId", openId);
        return "yuyue";
    }

    /**
     * 保存预约信息
     * @param appointment
     * @return
     */
    @PostMapping("/save")
    @ResponseBody
    public Object save(@RequestBody Appointment appointment) {
        try {
            appointmentService.save(appointment);
        } catch (BusinessException e) {
            LoggerUtils.error(AppointmentController.class, "预约信息保存失败");
            return new ResponseResult(e.getCode(), e.getMessage());
        }
        return new ResponseResult(CommonReturnCode.OK);
    }

    /**
     * 跳转到详情页面
     * @param id
     * @param model
     * @return
     */
    @RequestMapping("/detail")
    public String detail(@RequestParam("id") Integer id, Model model) {
        try {
            AppointmentVo appointment = appointmentService.findByAppointId(id);
            model.addAttribute("appoint", appointment);
        } catch (BusinessException e) {
            LoggerUtils.error(AppointmentController.class, e.getMessage());
        }
        return "/appointmentDetail";
    }

    /**
     * 取消预约信息
     * @param id
     * @return
     */
    @RequestMapping("/cancel")
    @ResponseBody
    public Object cancel(@RequestParam("id") Integer id) {
        try {
            appointmentService.cancelAppoint(id);
        } catch (BusinessException e) {
            LoggerUtils.error(AppointmentController.class, e.getMessage());
            return new ResponseResult(e.getCode(), e.getMessage());
        }
        return new ResponseResult(CommonReturnCode.OK);
    }
}
